package com.road.crawler.meizitu.crawler;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.SchedulerContext;
import com.road.crawler.meizitu.model.PictureInfo;
import com.road.crawler.meizitu.service.PictureInfoService;

/**
 * @Description :
 * @Author :WeiHui.Zhang
 * @Data : 2016/3/30 11:06
 * @Version:1.0.0
 */
@Service
public class NextPagePipeline implements Pipeline<SecondPage> {

	@Resource(name="pictureInfoServiceImpl")
	private PictureInfoService pictureInfoService;

	@Override
	public void process(SecondPage bean) {
		// 保存当前页爬取的图片信息
		List<Picture> pictures = bean.getPictures();
		if (pictures == null || pictures.size() == 0) {
			return ;
		}
		
		String siteRootUrl = bean.getRequest().getHeaders().get("Referer");
		
		for (Picture picture : pictures) {
			// 分类下的漫画列表挨个入库
			PictureInfo pictureInfo = new PictureInfo();
			pictureInfo.setTag(bean.getTagId());
			pictureInfo.setAlt(picture.getAlt());
			pictureInfo.setHref((siteRootUrl + picture.getHref()).replace("//", "/"));
			pictureInfo.setThumbnail(picture.getThumbnail());
			pictureInfo.setNewestTitle(picture.getNewestTitle());
			pictureInfo.setNewestUrl(picture.getNewestUrl());
			pictureInfo.setCreateDate(new Date());
			pictureInfoService.save(pictureInfo);
			
			// 继续抓取详情页
//			HttpRequest sub = bean.getRequest().subRequest(picture.getHref());
//			sub.addParameter("picInfoId", ""+pictureInfo.getId());
//			SchedulerContext.into(sub);
		}
		
		//继续抓取下一页构造下一页
		String nextPageUrl = bean.getNextPageUrl();
		if (StringUtils.isNotEmpty(nextPageUrl)) {
			HttpRequest sub = bean.getRequest().subRequest(nextPageUrl);
			sub.addParameter("tagId", "" + bean.getTagId());
			sub.addParameter("currPage", "" + (bean.getCurrPage() + 1));
			SchedulerContext.into(sub);
		}
	}
}
